<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
      error-text="网络不佳, 请稍后重试"
    >
      <van-cell v-for="item in resolveList" :key="item.ch_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getResolve } from '@/api/search.js'
export default {
  data () {
    return {
      resolveList: [],
      loading: false, // 加载完成后改为false 如果没有占满一个屏幕 就继续渲染
      finished: false,
      page: 1,
      error: false // 是否显示错误信息
    }
  },
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      try {
        const { data: res } = await getResolve({
          page: this.page,
          per_page: 10,
          q: this.searchText
        })
        console.log(res)
        this.resolveList.push(...res.data.results)
        this.loading = false
        if (res.data.results.length === 0) {
          this.finished = true
        } else {
          this.page++ // 如果返回长度不为空 说明还有数据 将page++ 请求下一页数据
        }
      } catch (e) {
        this.error = true
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>

</style>
